// 引入行为种类
import actionType from '../action/actionType'

import { message } from 'antd'

// 引入draw.js下的两个构造函数
import { DrawFunction, DrawGraph } from '../../js/draw'
// 初始化的状态
const initState = {
	drawList: [],
	toRedraw: false,
	basicInfo: {
		name: '',
		color: '#61dafb',
		width: 2,
	},
	show: false,
	type: '',
	detailedType: '0',
}
// 导出的reducer
export default function drawReducer(state = initState, action) {
	const { drawList } = state
	switch (action.type) {
		case actionType.SET_FUNC_WINDOW: {
			return {
				...state,
				show: true,
				type: 'func',
				detailedType: action.payload,
			}
		}
		case actionType.SET_GRAPH_WINDOW: {
			return {
				...state,
				show: true,
				type: 'graph',
				detailedType: action.payload,
			}
		}
		case actionType.CLOSE_WINDOW: {
			return {
				...state,
				show: false,
				// toRedraw: false,
				basicInfo: {
					name: '',
					color: '#61dafb',
					width: 2,
				},
			}
		}
		case actionType.UPDATE_BASIC_INFO: {
			return {
				...state,
				basicInfo: {
					...state.basicInfo,
					...action.basicInfo,
				},
			}
		}

		case actionType.ADD_FUNC_BY_PARAMS: {
			// 拿到参数并转换为解析式
			const { params, type } = action.payload
			const { name, color, width } = state.basicInfo
			const fae = turnParamsToFae(params, type)
			const drawFunc = new DrawFunction(name, fae, { color, width })
			// 做记录，使得可修改
			drawFunc.params = params
			drawFunc.type = type

			drawFunc.draw()
			message.success('绘制成功！')
			// 返回一个全新的状态，在drawList后连接新的函数
			return {
				...state,
				drawList: drawList.concat(drawFunc),
			}
		}

		case actionType.EDIT_FUNC_BY_PARAMS: {
			// 创建一个新实例
			const { params, type } = action.payload
			const { name, color, width } = state.basicInfo
			const fae = turnParamsToFae(params, type)
			const drawFunc = new DrawFunction(name, fae, { color, width })
			// 做记录，使得可修改
			drawFunc.params = params
			drawFunc.type = type
			const tmpList = [...drawList]
			// 将原先的替换为新的
			tmpList.splice(drawList.indexOf(action.payload.edit), 1, drawFunc)
			message.success('编辑成功！')
			return {
				...state,
				drawList: tmpList,
				toRedraw: true,
			}
		}

		case actionType.ADD_FUNC_BY_FAE: {
			const { fae, type } = action.payload
			const { name, color, width } = state.basicInfo
			const drawFunc = new DrawFunction(name, fae, { color, width })
			// 做记录，使得可修改
			drawFunc.type = type
			drawFunc.draw()
			message.success('绘制成功！')
			return {
				...state,
				drawList: drawList.concat(drawFunc),
			}
		}

		case actionType.EDIT_FUNC_BY_FAE: {
			const { fae, type } = action.payload
			const { name, color, width } = state.basicInfo
			const drawFunc = new DrawFunction(name, fae, { color, width })
			// 做记录，使得可修改
			drawFunc.type = type
			const tmpList = [...drawList]
			tmpList.splice(drawList.indexOf(action.payload.edit), 1, drawFunc)
			message.success('编辑成功！')
			return {
				...state,
				drawList: tmpList,
				toRedraw: true,
			}
		}

		case actionType.ADD_GRAPH: {
			const { params, type } = action.payload
			const { name, color, width } = state.basicInfo
			const drawGraph = new DrawGraph(name, params, type, { color, width })
			drawGraph.draw()
			message.success('绘制成功！')
			return {
				...state,
				drawList: drawList.concat(drawGraph),
			}
		}

		case actionType.EDIT_GRAPH: {
			const { params, type } = action.payload
			const { name, color, width } = state.basicInfo
			const drawGraph = new DrawGraph(name, params, type, { color, width })
			const tmpList = [...drawList]
			tmpList.splice(drawList.indexOf(action.payload.edit), 1, drawGraph)
			message.success('编辑成功！')
			return {
				...state,
				drawList: tmpList,
				toRedraw: true,
			}
		}

		case actionType.DELETE_ONE: {
			const tmpList = [...drawList]
			tmpList.splice(drawList.indexOf(action.payload), 1)
			message.success('删除成功！')
			return {
				...state,
				drawList: tmpList,
				toRedraw: true,
			}
		}

		case actionType.TOGGLE_HIDDEN_STATE: {
			const tmpObj = action.payload.cloneOneself()
			// 做记录，使得可修改
			tmpObj.params = action.payload.params
			tmpObj.type = action.payload.type
			tmpObj.show = !tmpObj.show
			const tmpList = [...drawList]
			tmpList.splice(drawList.indexOf(action.payload), 1, tmpObj)
			message.success('编辑成功！')
			return {
				...state,
				drawList: tmpList,
				toRedraw: true,
			}
		}

		case actionType.RESET_TO_REDRAW: {
			return {
				...state,
				toRedraw: false,
			}
		}
		// reset!!!
		case actionType.RESET_STORE: {
			return initState
		}
		default: {
			return state
		}
	}
}

function turnParamsToFae(params, type) {
	/**
	 * type:
	 * 	1: 一次函数
	 * 	2: 二次函数一般式
	 * 	-1: 反比例函数
	 * 	-2: 三角函数sin
	 * 	-3: 三角函数cos
	 * 	-4: 二次函数顶点式
	 */
	switch (type) {
		case 1: {
			return `${params[0]}x+${params[1]}`
		}
		case 2: {
			return `${params[0]}x^2+${params[1]}x+${params[2]}`
		}
		case -1: {
			return `${params[0]}/x`
		}
		case -2: {
			return `sin(${params[0]}x)`
		}
		case -3: {
			return `cos(${params[0]}x)`
		}
		case -4: {
			return `${params[0]}(x-${params[1]})^2+${params[2]}`
		}
		default: {
			return ''
		}
	}
}
